package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.ItemTransferAction;
import gatperdut.chato.core.interpreter.grammar.comminfo.ItemTransferInfo;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.core.item.inventory.InventoryController;

public class GetFromContainerAction extends ItemTransferAction {

    public GetFromContainerAction( ItemTransferInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        ItemController container = null;
        InventoryController inventoryControllerContainerIsIn = null;
        if ( !getInfo().isGround() ) {
            inventoryControllerContainerIsIn = getEntityController().getInventoryController();
        }
        if ( getInfo().isGround() || ( !getInfo().isGround() && inventoryControllerContainerIsIn == null ) ) {
            inventoryControllerContainerIsIn = getEntityController().getRoomController().getInventoryController();
        }

        synchronized ( inventoryControllerContainerIsIn ) {
            container = findInRoomOrInventory( getInfo().getFrom(), getInfo().isGround() );

            if ( container == null ) {
                getEntityController().tx( "You cannot find that container." );
                return CommandExecutionResult.OK;
            }
            if ( container.getInventoryController() == null ) {
                getEntityController().tx( container.getShortDesc() + " is not a container." );
                return CommandExecutionResult.OK;
            }
            if ( !container.getItem().getInventory().isOpen() ) {
                getEntityController().tx( container.getShortDesc() + " is closed." );
                return CommandExecutionResult.OK;
            }

            synchronized ( container.getInventoryController() ) {
                ItemController pick = container.getInventoryController().getItemController( getInfo().getWhat() );
                if ( pick == null ) {
                    getEntityController().tx( "You don't see that in " + container.getShortDesc() + "." );
                    return CommandExecutionResult.OK;
                }
                synchronized ( getEntityController().getInventoryController() ) {
                    if ( !getEntityController().getInventoryController().hasOneFreeHand() ) {
                        getEntityController().tx( "You'll need at least a free hand to do that." );
                        return CommandExecutionResult.OK;
                    }

                    container.getInventoryController().takeFromInventory( pick );

                    getEntityController().getInventoryController().take( pick );

                    getEntityController().getEchoes().txGetFromContainer( pick, container, emote );
                }
            }
        }
        return CommandExecutionResult.OK;
    }

}
